/**
 * 
 */
package com.gragra.sampling.vector;
import java.io.BufferedReader;
import java.io.IOException;
import com.gragra.data.MixingCorpus;
import com.gragra.data.MixingCorpusBaseImplementation;
import com.gragra.util.StringToInt;
/**
 * This class implements general utilities to generate collections of VectorStructure data.
 * @author Christoph Teichmann
 * 03.05.2013
 */
public class VectorCorpusReader
{
	/**
	 * this method is used to create a Mixing capable corpus of Vector structures from a BufferedReader.
	 * The format that is used for the vectors is as follows:
	 * name_1 unit_1^1|element_1^1|value_1^1 unit_1^2|element_1^2|value_1^2 ....
	 * name_2 unit_2^1|element_2^1|value_2^1 unit_2^2|element_2^2|value_2^2
	 * @return
	 * @throws IOException 
	 */
	public MixingCorpus<VectorStructure> createCorpusFromFile(BufferedReader input, int threads) throws IOException
	{
		MixingCorpus<VectorStructure> corp = new MixingCorpusBaseImplementation<VectorStructure>();
		String string;
		while((string = input.readLine()) != null)
		{
			String[] parts = string.split("\\s+");
			int name = StringToInt.ensure(parts[0]);
			int[][] codes = new int[parts.length-1][];
			double[] values = new double[parts.length-1];
			for(int i=1;i<parts.length;++i)
			{
				String[] subParts = parts[i].split("\\|");
				codes[i-1] = new int[] {Integer.parseInt(subParts[0]),Integer.parseInt(subParts[1])};
				values[i-1] = Double.parseDouble(subParts[2]);
			}
			corp.addData(new VectorStructure(name, values, codes, threads));
		}
		return corp;
	}
}